var app = getApp();
var getSign = require('../../utils/getSign/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseUrl:'',
    advimg: [],
    currentSwiper: 0,
    isAuthorizationShow: false,
    xieyi:[]
  },
  goTodutys: function (event)
  {
    var url = event.currentTarget.dataset['url'].replace('?', '$')
    url = url.replace(/=/g, '%')
    url = url.replace(/&/g, '@')
    wx.navigateTo({
      url: '/pages/outweb/outweb?url=' + url,
    })
  },
  //跳转到个人购买流程
  goToPrivateWay: function () {
    wx.navigateTo({
      url: '/pages/private/private'
    })
  },
  //跳转到企业购买流程
  goToEnterpriseWay: function () {
    wx.navigateTo({
      url: '/pages/enterprise/enterprise'
    })
  },
  goToSuccesssCases: function () {
    wx.navigateTo({
      url: '/pages/successCase/successCase'
    })
  },
  //授权获取用户信息
  onGotUserInfo: function (e) {
    if (e.detail.errMsg === "getUserInfo:ok") {
      app.userInfo = e.detail.userInfo
      console.log(app.userInfo, "app")
      wx.setStorage({
        key: 'userInfo',
        data: e.detail.userInfo
      })
      wx.showLoading({
        title: '登录中',
      })
      //授权成功
      this.setData({
        isAuthorizationShow: false
      })
      //登录
      wx.login({
        success(res) {
          if (res.code) {
            var url = 'https://wx-app-api.sjune.cn/yuren/API.ashx'
            var method = 'POST'
            var data = {
              'js_code': res.code,
              'Api_Type': '001',
            }
            var message = ''
            getSign.request(url, method, data, message).then(
              (res) => {
                //获取了session_key openid
                console.log(res, 'session_key openid')
                wx.setStorage({
                  key: 'openid',
                  data: JSON.parse(res.Data)
                })
                var name = app.userInfo.nickName;
                var nick = name.replace(/\uD83C[\uDF00-\uDFFF]|\uD83D[\uDC00-\uDE4F]/g, "口");
                var userinfos = {
                  'Api_Type': '002',
                  'openId': JSON.parse(res.Data).openid,
                  'avatarUrl': app.userInfo.avatarUrl,
                  'nickName': nick,
                  'sex': app.userInfo.gender,
                  'country': app.userInfo.country,
                  'province': app.userInfo.province,
                  'city': app.userInfo.city
                }
                //获取用户登录态
                getSign.request('https://wx-app-api.sjune.cn/yuren/API.ashx', 'POST', userinfos, '').then(
                  (res) => {
                    console.log(res, '登录态')
                    //存储memberInfo
                    wx.setStorage({
                      key: 'memberInfo',
                      data: res.Data
                    })
                    //跳转到首页
                    wx.switchTab({
                      url: '/pages/index/index',
                    })
                  },
                  (err) => {
                    console.log(err, '登录态失败')
                  }
                )
              },
              (err) => {
                console.log(err, '获取失败')
              }
            )
          } else {
            console.log('登录失败！' + res.errMsg)
          }
        }
      })

    } else {
      //未授权
      wx.showToast({
        title: '请授权',
        icon: 'loading',
        duration: 1000
      })
    }
  },
  swiperChange: function (e) {
    this.setData({
      currentSwiper: e.detail.current
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      baseUrl: app.baseUrl
    })
    
      //检查Session_key是否过期
      wx.checkSession({
        success: (res) => {
          //未过期
          console.log('session_key 未过期')
          var memberInfo = wx.getStorageSync('memberInfo')
          if (memberInfo.MemberToken) {
            wx.showToast({
              title: '正在登陆',
              icon: 'loading',
              duration: 500
            })
            setTimeout(function () {
              wx.switchTab({
                url: '/pages/index/index',
              })
            }, 500)
          } else {
            //过期
            this.setData({
              isAuthorizationShow: true
            })
          }
         
        },
        fail: (res) => {
          //过期
          this.setData({
            isAuthorizationShow: true
          })
        }

      })
    //发送请求
      var postArgs = {
        'Api_Type': '025',
        'page':1
        // 'Member_Id': memberInfo.MemberId,
        // 'Member_Token': memberInfo.MemberToken
      }
      //获取
      getSign.request('https://wx-app-api.sjune.cn/yuren/API.ashx', 'POST', postArgs, '').then(
        (res) => {
          console.log(res, '登录协议')
          this.setData({
            xieyi: res.Data,
          })
        },
        (err) => {
          console.log(err.Message, '协议失败')
        }
      )
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})